﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using CuaHangLaptop.BIZ;

namespace CuaHangLaptop.DAL
{
    class KhachHang_DAL
    {
        internal static int Get_Lasted_ID()
        {
            int id = 0;
            SqlConnection conn = Connection.Connection_String();
            SqlDataReader rdr = null;
            try
            {
                conn.Open();
                SqlCommand cmd = new SqlCommand("select id from KhachHang order by id desc", conn);
                rdr = cmd.ExecuteReader();
                rdr.Read();
                id = rdr.GetInt32(0);
            }
            catch
            {
            }
            finally
            {
                if (rdr != null) rdr.Close();
                conn.Close();
            }
            return id;
        }

        // SELECT * FROM NHA SAN XUAT
        internal static List<KhachHang_BIZ> GetAll_KH()
        {
            List<KhachHang_BIZ> result = new List<KhachHang_BIZ>();
            SqlConnection conn = Connection.Connection_String();
            SqlDataReader rdr = null;
            try
            {
                conn.Open();
                SqlCommand cmd = new SqlCommand("select * from KhachHang", conn);
                rdr = cmd.ExecuteReader();

                while (rdr.Read())
                {
                    KhachHang_BIZ dd = new KhachHang_BIZ(rdr.GetInt32(0), rdr.GetString(1), rdr.GetString(2), rdr.GetString(3),
                                                         rdr.GetString(4), rdr.GetString(5), rdr.GetString(6), rdr.GetString(7));
                    result.Add(dd);                                      
                }
            }
            finally
            {
                if (rdr != null) rdr.Close();
                conn.Close();
            }
            return result;
        }

        // LẤY NHÀ SẢN XUẤT CÓ MANSX = NSX.MANSX
        internal static KhachHang_BIZ Get_KhachHang_ByCode(string _maKH)
        {
            KhachHang_BIZ kh1 = new KhachHang_BIZ();
            SqlConnection conn = Connection.Connection_String();
            SqlDataReader rdr = null;
            try
            {
                conn.Open();
                String s = String.Format("select * from KhachHang where MaKH='{0}'", _maKH);
                SqlCommand cmd = new SqlCommand(s, conn);
                rdr = cmd.ExecuteReader();
                rdr.Read();
                if (rdr.HasRows)
                kh1 = new KhachHang_BIZ(rdr.GetInt32(0), rdr.GetString(1), rdr.GetString(2), rdr.GetString(3),
                                                         rdr.GetString(4), rdr.GetString(5), rdr.GetString(6), rdr.GetString(7));
            }
            finally
            {
                if (rdr != null) rdr.Close();
                conn.Close();
            }
            return kh1;
        }

        // DELETE
        internal static void Delete(KhachHang_BIZ kh)
        {
            SqlConnection conn = Connection.Connection_String();
            try
            {
                conn.Open();
                String s = String.Format("delete from Khachhang where id={0}", kh.ID);
                SqlCommand cmd = new SqlCommand(s, conn);

                if (cmd.ExecuteNonQuery() > 0)
                {
                }
            }
            finally
            {
                conn.Close();
            }
        }

        // UPDATE
        internal static void Update(KhachHang_BIZ kh)
        {
            SqlConnection conn = Connection.Connection_String();
            try
            {
                conn.Open();
                String s = String.Format("update  KhachHang set Makh = '{0}', TenKH = '{1}',DienThoai= '{2}',Email = '{3}', DiaChi= '{4}',TenDangNhap= '{5}',MatKhau= '{6}' where id={7}",kh.MaKH, kh.TenKH, kh.Sdt, kh.Email, kh.Diachi, kh.Username, kh.Pass, kh.ID);
                SqlCommand cmd = new SqlCommand(s, conn);

                if (cmd.ExecuteNonQuery() > 0)
                {
                }
            }
            finally
            {
                conn.Close();
            }
        }

        // INSERT
        internal static int Insert(KhachHang_BIZ kh)
        {
            int newId = -1;
            SqlConnection conn = Connection.Connection_String();
            SqlDataReader rdr = null;
            try
            {
                conn.Open();
                String s = String.Format("insert into KhachHang values('{0}', '{1}', '{2}', '{3}', '{4}', '{5}', '{6}')", kh.MaKH, kh.TenKH, kh.Sdt, kh.Email, kh.Diachi, kh.Username, kh.Pass);
                SqlCommand cmd = new SqlCommand(s, conn);

                if (cmd.ExecuteNonQuery() > 0)
                {
                    s = String.Format("select id from KhachHang where MaKH='{0}'", kh.MaKH);
                    cmd.CommandText = s;
                    rdr = cmd.ExecuteReader();

                    if (rdr.HasRows)
                    {
                        rdr.Read();
                        newId = Int32.Parse(rdr["id"].ToString());
                    }
                }
            }
            finally
            {
                if (rdr != null) rdr.Close();
                conn.Close();
            }
            return newId;
        }
    }
}
